package com.conradharrison.simplecamera2;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;


public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";
    private Camera mCamera;
    private CameraPreview mPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "In OnCreate");
        setContentView(R.layout.activity_main);

        // Create an instance of Camera
        mCamera = getCameraInstance();

        mPreview = new CameraPreview(this, mCamera);
        Log.d(TAG, "new CameraPreview");

        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_fragment);
        preview.addView(mPreview);
        Log.d(TAG, "Done OnCreate");
    }

    protected void onStart(){
        super.onStart();
        Log.d(TAG, "Done onStart");
    }

    protected void onRestart(){
        super.onRestart();
        Log.d(TAG, "Done onRestart");
    }

    protected void onResume(){
        super.onResume();
        mCamera = getCameraInstance();
        Log.d(TAG, "Done onResume");
    }

    protected void onPause(){
        super.onPause();
        releaseCamera();
        Log.d(TAG, "Done onPause");
    }

    protected void onStop(){
        super.onStart();
        Log.d(TAG, "Done onStop");
    }

    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "Done onDestroy");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            toggleActionBar();
        }
        return true;
    }

    private void toggleActionBar() {
        ActionBar actionBar = getActionBar();

        if(actionBar != null) {
            if(actionBar.isShowing()) {
                actionBar.hide();
            }
            else {
                actionBar.show();
            }
        }
    }

    /** A safe way to get an instance of the Camera object. */
    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open(); // attempt to get a Camera instance
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }
        return c; // returns null if camera is unavailable
    }

    private void releaseCamera(){
        if (mCamera != null){
            mCamera.release();        // release the camera for other applications
            mCamera = null;
        }
        Log.d(TAG, "Done release");
    }
}
